package com.ngc.mall.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ngc.mall.common.configure.response.ResponseResult;
import com.ngc.mall.domain.SaleAdDomain;
import com.ngc.mall.dto.AttachmentDTO;
import com.ngc.mall.dto.SaleAdDTO;
import com.ngc.mall.param.SaleAdParam;
import com.ngc.mall.service.ISaleAdService;
import com.ngc.mall.vo.SaleAdVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 *  营销-广告模块
 *
 * @author : Chen Xiao Lin
 * @Date : 2023/3/30 9:31
 **/

@RestController
@RequestMapping("/sale/ad")
@Api(tags = "广告")
public class SaleAdController {
    @Resource
    private ISaleAdService saleAdService;

    /**
     * 获取所有广告信息
     */
    @GetMapping("/list")
    @ApiOperation(value = "获取所有广告信息")
    public ResponseResult<List<SaleAdVO>> selectAllAd(){
        return ResponseResult.success(saleAdService.selectAll());
    }

    /**
     * 新增广告
     */
    @PostMapping("")
    @ApiOperation(value="新增广告")
    public ResponseResult<Long>  createAd(@RequestBody SaleAdParam param){
        SaleAdDTO dto=SaleAdDTO
                .builder()
                .isEnabled(param.getIsEnabled())
                .sort(param.getSort())
                .note(param.getNote())
                .name(param.getName())
                .expireTime(param.getExpireTime())
                .img(param.getImg())
                .build();
        return ResponseResult.success(saleAdService.createSaleAd(dto));
    }

    /**
     * 更新广告
     */
    @PutMapping("/{id}")
    public ResponseResult<Boolean> updateAdById(@PathVariable Long id,@RequestBody SaleAdParam param){
        SaleAdDTO dto=SaleAdDTO
                .builder()
                .isEnabled(param.getIsEnabled())
                .sort(param.getSort())
                .note(param.getNote())
                .name(param.getName())
                .expireTime(param.getExpireTime())
                .img(param.getImg())
                .build();
        return ResponseResult.success(saleAdService.updateById(dto,id));
    }

    /**
     * 删除广告
     */
    @DeleteMapping("/{id}")
    public ResponseResult<Boolean> deleteById(@PathVariable Long id){
        return ResponseResult.success(saleAdService.removeById(id));
    }

    /**
     * 下线广告
     */
    @PutMapping("/offline/{id}")
    public Boolean updateToOffline(@PathVariable Long id){
        return saleAdService.updateAdStatus(false,id);
    }
    /**
     * 上线广告
     */
    @PutMapping("/online/{id}")
    public Boolean updateToOnline(@PathVariable Long id){
        return saleAdService.updateAdStatus(true,id);
    }

    /**
     * 分页查询广告信息
     */
    @GetMapping("/list/{current}/{size}")
    public IPage<SaleAdDomain> getPages(@PathVariable int current,@PathVariable int size){
        return saleAdService.getPageList(current,size);
    }
    /**
     * 根据id获取广告
     * @return
     */
    @GetMapping("/{id}")
    public ResponseResult<SaleAdVO> getAdById(@PathVariable Long id){
        return ResponseResult.success(saleAdService.getAdById(id));
    }
}
